<template>
	<view class="container">
		<!-- 顶部导航栏 -->
		<view class="nav-bar">
			<view class="nav-item" :class="{ active: activeTab === 'topics' }" @tap="switchTab('topics')">
				<uni-icons class="icon" type="chatboxes-filled" size="20"></uni-icons>
				<text>话题交流</text>
			</view>
			<view class="nav-item" :class="{ active: activeTab === 'tutoring' }" @tap="switchTab('tutoring')">
				<uni-icons class="icon" type="person-filled" size="20"></uni-icons>
				<text>预约辅导</text>
			</view>
			<!-- <view class="nav-item" @tap="showPostDialog">
				<uni-icons class="icon" type="paperplane-filled" size="20"></uni-icons>
				<text>话题发布</text>
			</view> -->
		</view>
		<view class="topicBox" v-if="activeTab==='topics'" @click="showPostDialog">
			<uni-icons class="sendIcon" type="paperplane-filled" color="#FFFFFF" size="20"></uni-icons>
			<text>发布话题</text>
		</view>
		<!-- 话题列表 -->
		<scroll-view v-if="activeTab === 'topics'" scroll-y class="content-area">
			<view v-for="(topic, index) in topics" :key="index" class="topic-card" @click="viewTopicDetail(topic)">
				<view class="topic-title">{{ topic.title }}</view>
				<view class="topic-content">{{ topic.content }}</view>
				<view class="topic-footer">
					<text class="author">{{ topic.username }}</text>
					<text class="separator">|</text>
					<text class="date">{{ topic.createdAt.slice(0,10) }}</text>
				</view>
			</view>
		</scroll-view>

		<!-- 辅导者列表 -->
		<scroll-view v-if="activeTab === 'tutoring'" scroll-y class="content-area">
			<view v-for="(tutor, index) in tutors" :key="index" class="tutor-card">
				<view class="tutor-header">
					<text class="tutor-name">{{ tutor.name }}</text>
					<text class="tutor-rating">评分: 4.9</text>
				</view>
				<view class="tutor-info">
					<text>学历: {{ tutor.education }}</text>
					<text>毕业院校: {{ tutor.graduationSchool }}</text>
					<text>证书: {{ tutor.certificateName }}</text>
				</view>
				<button class="consult-btn" @tap="startConsultation(tutor)">预约咨询</button>
			</view>
		</scroll-view>

		<!-- 发布话题弹窗 -->
		<uni-popup ref="postPopup" type="center">
			<view class="post-dialog">
				<view class="dialog-header">
					<text>发布新话题</text>
				</view>
				<textarea v-model="newPost.content" class="post-input" placeholder="请输入话题内容..." />
				<view class="dialog-footer">
					<button class="cancel-btn" @tap="closePostDialog">取消</button>
					<button class="submit-btn" @tap="submitPost">发布</button>
				</view>
			</view>
		</uni-popup>

		<!-- 聊天窗口 -->
		<uni-popup ref="chatPopup" type="center">
			<view class="chat-window">
				<view class="chat-header">
					<text>与 {{ currentTutor.name }} 的对话</text>
					<text class="close-icon" @tap="closeChat">×</text>
				</view>
				<scroll-view scroll-y class="chat-messages" :scroll-top="scrollTop">
					<view v-for="(msg, index) in chatMessages" :key="index" class="message-item outgoing-message">
						<view class="message-content">
							{{ msg.content }}
						</view>
					</view>
				</scroll-view>
				<view class="chat-footer">
					<input v-model="messageInput" class="message-input" placeholder="请输入消息..." />
					<button class="send-btn" @tap="submitMessage">发送</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getTopicList,
		getDoctorList,
		sendMessage
	} from "@/api/index.js"
	export default {
		data() {
			return {
				activeTab: 'topics',
				topics: [],
				tutors: [{
					name: '王教授',
					rating: 4.9,
					education: '医学博士',
					school: 'XXX医科大学',
					certificates: '执业医师资格证'
				}],
				newPost: {
					content: ''
				},
				currentTutor: {},
				messageInput: '',
				chatMessages: [],
				scrollTop: 0,
				userInfo: {}
			}
		},
		async onLoad() {
			let uInfo = uni.getStorageSync('mini-userinfo')
			if (uInfo) {
				this.userInfo = uInfo
			}
			const list = await getTopicList()
			this.topics = list.data.data
			const doctorList = await getDoctorList()
			this.tutors = doctorList.data.data
			console.log(doctorList, '????????????');
		},
		methods: {
			switchTab(tab) {
				this.activeTab = tab
			},

			showPostDialog() {
				uni.navigateTo({
					url: '/pages/community/send'
				})
			},

			closePostDialog() {
				this.$refs.postPopup.close()
				this.newPost.content = ''
			},

			submitPost() {
				if (!this.newPost.content.trim()) {
					uni.showToast({
						title: '请输入内容',
						icon: 'none'
					})
					return
				}

				uni.showToast({
					title: '发布成功',
					icon: 'success'
				})
				this.closePostDialog()
			},

			viewTopicDetail(topic) {
				// const params = encodeURIComponent(JSON.stringify(topic));
				uni.navigateTo({
					url: `/pages/community/detail?topicId=${topic.id}`
				})
			},

			startConsultation(tutor) {
				this.currentTutor = tutor
				this.chatMessages = []
				this.$refs.chatPopup.open()
			},

			closeChat() {
				this.$refs.chatPopup.close()
				this.messageInput = ''
				this.chatMessages = []
				this.currentTutor = {}
			},

			async submitMessage() {
				if (!this.messageInput.trim()) return
				const params = {
					sender_id: this.userInfo.id,
					receiver_id: this.currentTutor.user.id,
					content: this.messageInput,
					message_type: "user_to_doctor",
				}
				const res = await sendMessage(params)
				if (res.statusCode === 200) {
					this.chatMessages.push({
						content: this.messageInput
					})
					this.messageInput = ''
					uni.showToast({
						title: '咨询成功',
						icon: 'none'
					})
				}
				console.log(res, '评论？？');
			},

			endConsultation() {
				uni.showModal({
					title: '结束咨询',
					content: '请为辅导者评分(1-5分)',
					editable: true,
					success: (res) => {
						if (res.confirm) {
							const rating = Number(res.content)
							if (rating >= 1 && rating <= 5) {
								uni.showToast({
									title: '评分成功',
									icon: 'success'
								})
								this.closeChat()
							} else {
								uni.showToast({
									title: '请输入1-5的分数',
									icon: 'none'
								})
							}
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		min-height: 100vh;
		background: #f5f5f5;
	}

	.nav-bar {
		display: flex;
		justify-content: space-around;
		padding: 20rpx;
		background: #fff;
		border-bottom: 1rpx solid #eee;
	}

	.nav-item {
		display: flex;
		/* flex-direction: column; */
		align-items: center;

		.icon {
			margin-right: 4rpx;
		}
	}

	.topicBox {
		z-index: 999;
		position: fixed;
		right: 30rpx;
		bottom: 240rpx;
		width: 198rpx;
		height: 76rpx;
		background: #000000;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 10rpx;
		padding: 0 10rpx;

		.sendIcon {
			margin-right: 4rpx;
		}
	}

	.nav-icon {
		width: 48rpx;
		height: 48rpx;
		margin-bottom: 8rpx;
	}

	.active {
		color: #007AFF;
	}

	.content-area {
		box-sizing: border-box;
		width: 750rpx;
		height: calc(100vh - 120rpx);
		padding: 20rpx;
	}

	.topic-card {
		background: #fff;
		padding: 30rpx;
		margin-bottom: 20rpx;
		border-radius: 12rpx;
	}

	.topic-title {
		font-size: 32rpx;
		font-weight: bold;
		margin-bottom: 16rpx;
	}

	.topic-content {
		font-size: 28rpx;
		color: #666;
		margin-bottom: 16rpx;
		line-height: 1.6;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		word-break: break-all;
	}

	.topic-footer {
		font-size: 24rpx;
		color: #999;
	}

	.separator {
		margin: 0 16rpx;
	}

	.tutor-card {
		background: #fff;
		padding: 30rpx;
		margin-bottom: 20rpx;
		border-radius: 12rpx;
	}

	.tutor-header {
		display: flex;
		justify-content: space-between;
		margin-bottom: 16rpx;
	}

	.tutor-name {
		font-size: 32rpx;
		font-weight: bold;
	}

	.tutor-rating {
		color: #f0ad4e;
	}

	.tutor-info {
		display: flex;
		flex-direction: column;
		gap: 8rpx;
		margin-bottom: 20rpx;
		font-size: 28rpx;
		color: #666;
	}

	.consult-btn {
		background: #007AFF;
		color: #fff;
		border: none;
		border-radius: 8rpx;
	}

	.post-dialog {
		background: #fff;
		width: 600rpx;
		border-radius: 12rpx;
		padding: 30rpx;
	}

	.dialog-header {
		text-align: center;
		font-size: 32rpx;
		font-weight: bold;
		margin-bottom: 30rpx;
	}

	.post-input {
		width: 100%;
		height: 300rpx;
		border: 1rpx solid #eee;
		border-radius: 8rpx;
		padding: 20rpx;
		margin-bottom: 30rpx;
	}

	.dialog-footer {
		display: flex;
		justify-content: space-between;
		gap: 20rpx;
	}

	.cancel-btn,
	.submit-btn {
		flex: 1;
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
		border-radius: 8rpx;
		border: none;
	}

	.cancel-btn {
		background: #f5f5f5;
		color: #666;
	}

	.submit-btn {
		background: #007AFF;
		color: #fff;
	}

	.chat-window {
		background: #fff;
		width: 80vw;
		height: 80vh;
		border-radius: 12rpx;
		display: flex;
		flex-direction: column;
	}

	.chat-header {
		padding: 20rpx;
		border-bottom: 1rpx solid #eee;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.chat-container {
		max-height: 400px;
		overflow-y: auto;
		padding: 10px;
	}

	.message-item {
		margin-bottom: 10px;
		display: flex;
		justify-content: flex-end;
	}

	.outgoing-message .message-content {
		background-color: #9EEA6A;
		/* 类似 QQ 蓝色消息框颜色 */
		color: white;
		border-radius: 10px;
		padding: 10px;
		max-width: 70%;
		position: relative;
	}

	.close-icon {
		font-size: 40rpx;
		color: #999;
	}

	.chat-messages {
		flex: 1;
		padding: 20rpx;
		box-sizing: border-box;
	}

	.message {
		max-width: 70%;
		padding: 16rpx;
		margin-bottom: 16rpx;
		border-radius: 8rpx;
		word-break: break-all;
	}

	.sent {
		background: #007AFF;
		color: #fff;
		align-self: flex-end;
		margin-left: auto;
	}

	.received {
		background: #f5f5f5;
		color: #333;
	}

	.chat-footer {
		padding: 20rpx;
		border-top: 1rpx solid #eee;
		display: flex;
		gap: 20rpx;
	}

	.message-input {
		flex: 1;
		height: 80rpx;
		border: 1rpx solid #eee;
		border-radius: 8rpx;
		padding: 0 20rpx;
	}

	.send-btn {
		width: 160rpx;
		height: 80rpx;
		line-height: 80rpx;
		background: #007AFF;
		color: #fff;
		border: none;
		border-radius: 8rpx;
	}

	.end-btn {
		margin: 20rpx;
		background: #ff4d4f;
		color: #fff;
		border: none;
		border-radius: 8rpx;
	}
</style>